package org.meteorshower.employee_depart.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.*;
import org.apache.ibatis.type.EnumTypeHandler;
import org.meteorshower.employee_depart.util.enum_util.PoliticsEnum;
import org.meteorshower.employee_depart.util.enum_util.EducationEnum;
import org.meteorshower.employee_depart.util.enum_util.PersonalStatusEnum;
import org.meteorshower.employee_depart.util.enum_util.SexEnum;
import org.meteorshower.employee_depart.util.enum_util.WedLockEnum;
import org.meteorshower.system_depart.entity.Role;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serial;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 公司职员，映射完成
 * @table employee
 * 重写resultMap可能允许直接调用内置方法映射(未完成)
 * */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Employee implements UserDetails, Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    /**
     * ID
     */
    private String id;
    /**
     * 员工编号
     * */
    private String workId;
    /**
     * 员工密码
     * */
    private String password;
    /**
     * 身份证号码
     */
    @TableField("id_card")
    private String idCard;
    /**
     * 姓名
     */
    @TableField("name")
    private String name;
    /**
     * 性别
     */
//    @TableField(value = "sex",typeHandler = EnumTypeHandler.class)
    private SexEnum sex;
    /**
     * 年龄
     */
    @TableField("age")
    private Integer age;
    /**
     * 民族
     * */
    @TableField("nation")
    private Nation nation;
    /**
     * 是否已婚
     * */
    @TableField(value = "wedlock",typeHandler = EnumTypeHandler.class)
    private WedLockEnum wedlock;
    /**
     * 籍贯
     * */
    @TableField("native_place")
    private String nativePlace;
    /**
     * 手机号码
     * */
    @TableField("phone")
    private String phone;
    /**
     * 邮箱
     * */
    @TableField("email")
    private String email;
    /**
     * 政治面貌
     * */
    @TableField(value = "politic",typeHandler = EnumTypeHandler.class)
    private PoliticsEnum politic;
    /**
     * 股份
     */
    @TableField("equity")
    private Float equity;
    /**
     * 工资
     */
    @TableField("salary")
    private Integer salary;
    /**
     * 学历
     */
    @TableField(value = "education",typeHandler = EnumTypeHandler.class)
    private EducationEnum education;
    /**
     * 毕业学校
     * */
    @TableField("school")
    private String school;
    /**
     * 毕业专业
     * */
    @TableField("specialty")
    private String specialty;
    /**
     * 生日
     */
    @TableField("birthday")
    private Date birthday;
    /**
     * 入职时间
     */
    @TableField("hire_date")
    private Date createDate;
    /**
     * 员工状态
     */
    @TableField(value = "status",typeHandler = EnumTypeHandler.class)
    private PersonalStatusEnum status;
    /**
     * 员工描述
     */
    @TableField("remark")
    private String remark;
    /**
     * 员工地址
     */
    @TableField("address")
    private String address;
    /**
     * 工龄
     * */
    @TableField("work_age")
    private Integer workAge;
    /**
     * 部门ID
     * */
    @TableField("department")
    private Department department;
    /**
     * 经理
     * */
    @TableField("parent_id")
    private Employee parentLevel;
    /**
     * 职位等级
     * */
    @TableField("job_level")
    private JobLevel jobLevel;
    /**
     * 职位
     * */
    @TableField("position")
    private Position position;
    /**
     * 推荐人
     * */
    @TableField("referrer")
    private Employee referrer;
    /**
     * 合同
     * */
    @TableField(exist = false)
    private List<EmpContract> contractList;
    /**
     * 奖惩记录
     * */
    @TableField(exist = false)
    private List<EmpRewardAndPunish> empRewardAndPunishList;
    /**
     * 权限表
     * */
    @TableField(exist = false)
    private List<Role> roles;
    /**
     * 培训记录
     * */
    @TableField(exist = false)
    private List<EmpTrain> trainList;
    /**
     * 调岗记录
     * */
    @TableField(exist = false)
    private List<EmpSchedule> employeeremoveList;
    /**
     * 考评记录
     * */
    @TableField(exist = false)
    private List<EmpAppraise> appraiseList;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new CopyOnWriteArrayList<>();
        for (Role role : roles){
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }

    @Override
    public String getUsername() {
        return workId;
    }
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
